package com.tg.app.helper;

import com.tg.data.http.entity.WatchTime;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CarWatchTimeHelper {
    public static boolean isCarPrimaryWatchEnd(WatchTime watchTime){
        if (watchTime!=null){
            if (!isCarPrimaryWatchExpired(watchTime)&&(watchTime.getTotal()-watchTime.getUsed()>0)){
                return false;
            }
            return true;
        }

        return false;
    }
    public static boolean isCarPrimaryWatchExpired(WatchTime watchTime){
        long ms = System.currentTimeMillis()/1000;
        if (watchTime!=null&&watchTime.getExpiration_time()<ms){
            return true;
        }
        return  false;
    }
    public static void watchTimeQuery(String uuid,OnCarWatchTimeQueryCallBack callBack){
        HashMap<String,String> map = new HashMap<>();
        map.put("uuid",uuid);
        TGHttp.getInstance().watchTimeQuery(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<WatchTime>() {
                    @Override
                    protected void onSuccess(WatchTime content) {
                        if (callBack!=null){
                            callBack.onWatchTime(content);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack!=null){
                            callBack.onWatchTime(null);
                        }
                    }

                    @Override
                    protected void onOtherError(String error) {
                        super.onOtherError(error);
                        if (callBack!=null){
                            callBack.onWatchTime(null);
                        }
                    }
                });
    }

    public static interface OnCarWatchTimeQueryCallBack{
        void onWatchTime(WatchTime watchTime);
    }
}
